package com.hss.cn

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.lxj.xpopup.XPopup
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        initView()
        initListener()
    }

    private fun initView() {
        refreshData()
    }

    private fun initListener() {
        // 投丢+1
        btnLoseAdd.setOnClickListener {
            val data = ShootData()
            data.loseNbr = (tvLoseNbr.text.toString().toInt() + 1)
            data.hitNbr = tvHitNbr.text.toString().toInt()
            data.sum = (tvTodaySum.text.toString().toInt() + 1)
            ShootHelper.saveMsg(data)
            refreshData()
        }
        // 投丢-1
        btnLoseSub.setOnClickListener {
            val data = ShootData()
            data.loseNbr = (tvLoseNbr.text.toString().toInt() - 1)
            data.hitNbr = tvHitNbr.text.toString().toInt()
            data.sum = (tvTodaySum.text.toString().toInt() - 1)
            ShootHelper.saveMsg(data)
            refreshData()

        }
        // 命中+1
        btnHitAdd.setOnClickListener {
            val data = ShootData()
            data.hitNbr = (tvHitNbr.text.toString().toInt() + 1)
            data.loseNbr = tvLoseNbr.text.toString().toInt()
            data.sum = (tvTodaySum.text.toString().toInt() + 1)
            ShootHelper.saveMsg(data)
            refreshData()

        }
        // 命中-1
        btnHitSub.setOnClickListener {
            val data = ShootData()
            data.hitNbr = (tvHitNbr.text.toString().toInt() - 1)
            data.loseNbr = tvLoseNbr.text.toString().toInt()
            data.sum = (tvTodaySum.text.toString().toInt() - 1)
            ShootHelper.saveMsg(data)
            refreshData()

        }

        // 退出
        btnExit.setOnClickListener {
            finish()
        }

        // 今日清零
        btnClear.setOnClickListener {
            XPopup.Builder(this).asConfirm("温馨提醒", "确定清空今日数据吗？") {
                ShootHelper.clearTodayData()
                refreshData()
            }.show()
        }
    }

    /**
     * 更新数据
     */
    private fun refreshData() {
        // 获取今日数据
        if (ShootHelper.getTodayData().size > 0){
            val data = ShootHelper.getTodayData().get(0)
            tvTodayRate.text = "${data.hitRate}%"
            tvHitNbr.text = data.hitNbr.toString()
            tvLoseNbr.text = data.loseNbr.toString()
            tvTodaySum.text = data.sum.toString()
        }

        // 最差命中率
        ShootHelper.getWorstRate().run {
            tvWorstRecord.text = "${this}%"
        }

        // 最佳命中率
        ShootHelper.getBestRate().run {
            tvBestRecord.text = "${this}%"
        }

        // 生涯命中率
        ShootHelper.getAverage().run {
            tvRate.text = "${this}%"
        }

        // 总投篮数
        ShootHelper.getSum().run {
            tvSum.text = this.toString()
        }

        // 总命中数
        ShootHelper.getHitSum().run {
            tvHitSum.text = this.toString()
        }
    }
}